package com.atdel.demo25;


import com.atdel.demo23.Student;
import com.atdel.demo24.UserInfo;

import java.io.ObjectStreamException;
import java.lang.reflect.Field;

/**
 * @BelongsProject: java_study
 * @BelongsPackage: com.atdel.demo25
 * @Author: Del
 * @CreateTime: 2025-03-19  12:01
 * @Description: TODO
 * @Version: 1.0
 */
public class BeanUtil {

    /***
     * @author: Del
     * @date: 2025/3/19 12:04
     * @param: [bean：bean对象, propertyName：属性名, value：属性值]
     * @return: void
     * @description:
     **/
    public static void serProperty(Object bean, String propertyName, Object value) {
        try {
            // 获取类对象
            Class<?> clz = bean.getClass();
            // 获取属性值
            Field field = clz.getDeclaredField(propertyName);
            // 设置属性的访问权限
            field.setAccessible(true);
            // 设置属性的值
            field.set(bean, value);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    /***
     * @author: Del
     * @date: 2025/3/19 12:04
     * @param: [bean, propertyName]
     * @return: java.lang.Object
     * @description:
     **/
    public static Object getProperty(Object bean, String propertyName) {
        // 判断对象是否为空
        if (bean == null) {
            return null;
        }
        // 获取属性值
        Object value = null;
        try {
            // 获取类对象
            Class<?> aClass = bean.getClass();
            // 获取属性
            Field field = aClass.getDeclaredField(propertyName);
            // 设置属性的访问权限
            field.setAccessible(true);
            // 获取属性的值
            value = field.get(bean);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return value;
    }

    /***
     * @author: Del
     * @date: 2025/3/19 12:04
     * @param: [source, target]
     * @return: void
     * @description: 复制某个对象的属性到另一个对象
     **/
    public static void copyProperties(Object source, Object target) {
        try {
            Class<?> sourceClass = source.getClass();
            Class<?> targetClass = target.getClass();

            // 遍历源对象的所有字段
            for (Field field : sourceClass.getDeclaredFields()) {
                // 设置源字段为可访问
                field.setAccessible(true);
                // 获取源字段的值
                Object value = field.get(source);

                // 检查目标对象是否包含同名字段
                try {
                    Field targetField = targetClass.getDeclaredField(field.getName());
                    // 设置目标字段为可访问
                    targetField.setAccessible(true);
                    // 设置目标字段的值
                    targetField.set(target, value);
                } catch (NoSuchFieldException e) {
                    // 如果目标对象中不存在该字段，则忽略
                    System.out.println("字段 " + field.getName() + " 在目标对象中不存在，已跳过。");
                }
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    public static void main(String[] args) {
        // 测试方法
        Student student = new Student(1002,"李四", 36, 99.0);
        UserInfo userInfo = new UserInfo(1001, "张三", 20);

        copyProperties(student, userInfo);
        
        System.out.println(userInfo);
        System.out.println(student);

        // 设置属性
        serProperty(userInfo, "userName", "王五");
        serProperty(userInfo, "age", 22);

        System.out.println(userInfo);


        // 获取属性
        System.out.println(getProperty(userInfo, "userName"));
        System.out.println(getProperty(userInfo, "age"));
    }
}
